package com.zhouqinuan.spring.cloud.develop.framework.web.config.i18n;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

@Configuration
@EnableConfigurationProperties(InternationalizationProperties.class)
@ConditionalOnProperty(prefix = "i18n", name = "messages", havingValue = "enable", matchIfMissing = false)
public class InternationalizationConfiguration {
    
    @Autowired
    private InternationalizationProperties internationalizationProperties;
    
    /**
     * 扩展点，解析locale信息，可以是AcceptHeaderLocaleResolver，CookieLocaleResolver，SessionLocaleResolver
     * AcceptHeaderLocaleResolver：直接从Http请求的Header中通过accept-language获取locale信息
     * CookieLocaleResolver： 从cookie中获取，如果获取不到，也是通过accept-language获取locale信息
     * SessionLocaleResolver：从session中获取，如果获取不到，也是通过accept-language获取locale信息
     * MyLocaleResolver：从请求参数中取locale信息，默认是中文
     */
    @ConditionalOnMissingBean(LocaleResolver.class)
    @Bean(name = DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME)
    public LocaleResolver localeResolver() {

        if (internationalizationProperties.getLocaleResolver().equals("CookieLocaleResolver")) {
            return new CookieLocaleResolver();
        }

        if (internationalizationProperties.getLocaleResolver().equals("SessionLocaleResolver")) {
            return new SessionLocaleResolver();
        }

        if (internationalizationProperties.getLocaleResolver().equals("MyLocaleResolver")) {
            return new MyLocaleResolver();
        }

        return new AcceptHeaderLocaleResolver();
    }
    
    /**
     * 扩展点，国际化消息来源
     * ResourceBundleMessageSource：基于java的ResourceBundle实现了国际化，配置文件必须放在classpath下
     * ReloadableResourceBundleMessageSource：直接使用读取文件的方式实现国际化，规则跟java的相同，支持动态修改后刷新配置，避免在业务不能中断的情况下重启进程。配置文件可以放在任意目录下，指定目录后，该类会去指定目录中加载配置文件。
     * 自定义：可以读取数据库配置
     * 
     */
    @Bean
    public MessageSource messageSource() {
        
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename(internationalizationProperties.getBasename());
        messageSource.setDefaultEncoding(internationalizationProperties.getEncoding());
        messageSource.setCacheMillis(internationalizationProperties.getCacheSeconds());

        return messageSource;
    }
    
    @Bean
    public LocaleMessageService localeMessageService() {
        
        return new LocaleMessageService(messageSource());
    }
}
